/**
 * Copyright (C) 2015 kmhc-framework-base Project
 *               Author: Chris
 *               Date: 2015年11月23日
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.zmq.ha.client.runnable;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSON;

import com.zmq.ha.client.core.Context;
import com.zmq.ha.client.resource.RetryableContext;

/**
 * Name: RmqKeepAliveRunnable.java
 * ProjectName: [kmhc-framework-base]
 * Package: [com.kmhc.framework.rabbitmq.recover.runnable.RmqKeepAliveRunnable.java]
 * Description: TODO  
 * 
 * @since JDK1.7
 * @see
 *
 * @author: Chris
 * @date: 2015年11月23日 下午4:12:22
 *
 * Update-User: @author
 * Update-Time:
 * Update-Remark:
 * 
 * Check-User:
 * Check-Time:
 * Check-Remark:
 * 
 * Company: kmhc
 * Copyright: kmhc
 */
public class RmqKeepAliveRunnable extends RetryableContext  implements Runnable {
	
	private static final Logger log = LoggerFactory.getLogger(RmqKeepAliveRunnable.class);

	/** 
	 * @Title: RmqKeepAliveRunnable
	 * @Description: TODO
	 * @param          
	 * @throws 
	 */ 
	public RmqKeepAliveRunnable() {
		super();
		Thread thread = new Thread(this);
		thread.setName("RMQ-KeepAliveHTTPCheckRunnable");
		thread.start();
	}

	@Override
	public void run() {
		while( true ){
			long beginTime = System.currentTimeMillis();
			try {
				
				if( isOverTetryCount() ){
					CloseableHttpClient httpClient = HttpClients.createDefault();
					
					HttpGet get = new HttpGet(Context.keepAliveAddress);
					
					CloseableHttpResponse response = httpClient.execute(get);
					
					HttpEntity entity = response.getEntity();
					
					String content = EntityUtils.toString(entity);
					
					httpContentMsg msg = JSON.parseObject(content, httpContentMsg.class);
					
					if( "ok".equals( msg.getStatus() ) )
						signalForRetry();
				}
				long gapTime;
				if( (gapTime = ( 3000 - System.currentTimeMillis()) +beginTime ) > 0  ){
					try {
						Thread.sleep(gapTime);
					} catch (InterruptedException e1) {
						e1.printStackTrace();
					}
				}
				
			} catch (Exception e) {
				log.error("RMQ Server is close... retry after 3s...");
				long gapTime;
				//休眠3s
				if( (gapTime = ( 3000 - System.currentTimeMillis()) +beginTime ) > 0  ){
					try {
						Thread.sleep(gapTime);
					} catch (InterruptedException e1) {
						e1.printStackTrace();
					}
				}
			}
		}

	}

}

class httpContentMsg{
	private String status;

	public String getStatus() {
		return status;
	}
	public void setStatus(String status) {
		this.status = status;
	}
	
}
